#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""TIBET data format
Copyright (c) 2011-2014 Christian T. Steigies <steigies@physik.uni-kiel.de>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

This module offers functions to read NM data in ASCII format as used in Australia
and return data in the format as used by NMDB."""

__author__ = "Christian T. Steigies <steigies@physik.uni-kiel.de>"
__license__ = "GPL License"

import os
from datetime import datetime
#import urllib2

from nmdb.station.station_data import check_data, check_pressure
from nmdb.tools.datetool import valid_date

tibet_station = ["tibt"]
tibet_longnames = {"tibt": "Tibet"}

## --------------------------------------------------------------------------
def read_rt(options, station, data_type, year, month, day):
    """read realtime data

    options     verbose, dryrun, ...
    station     station short name, must be valid station
    data_type   "1m", "1h", "env", "meta"
    year        integer (1950-9999), may not be None
    month       integer (1-12), if None, read month 1-12
    day         integer (1-31), if None, read day 1-31

    return list of values in nmdb format
    """

    if station in tibet_station:
        pass
    else:
        raise ValueError  # wrong station

    value = []
    if valid_date(year, month, day):
        value = read_corr(options, station, data_type, year, month, day)

    return value

## --------------------------------------------------------------------------
def read_rev(options, station, data_type, year, month, day):
    """read revised data

    options     verbose, dryrun, ...
    station     station short name, must be valid station
    data_type   "1m", "1h", "env", "meta"
    year        integer (1950-9999), may not be None
    month       integer (1-12), if None, read month 1-12
    day         integer (1-31), if None, read day 1-31

    return list of values in nmdb format
    """

    if station in tibet_station:
        pass
    else:
        raise ValueError  # wrong station

    value = []
    if valid_date(year, month, day):
        value = read_corr(options, station, data_type, year, month, day)

    return value

## --------------------------------------------------------------------------
def read_corr(options, station, data_type, year, month, day):
    """
2013/10/03 00:00:00  188131  73337  23976  8726  3597  1599  864  494  845  606.89  19.12  181889  71910  23238  8414  3457  1535  830  473  809

    data:
        /database/TibetNM/01h_corr/yyyymm/yyyymmdd.txt
    filename:
        corr1m_20131003.txt
        corr1h_20131003.txt
    data format:
### pressure corrected minute data                                                                                            
### /01m_corr/yyyymm/yyyymmdd.txt                                                                                             
format description                                                                                                            
------ --------------------------                                                                                             
a19    start time of record                                                                                                   
------ pressure corrected -------                                                                                             
i8     total counts   [count/min]                                                                                             
i7     multiplicity 1 [count/min]                                                                                             
i7     multiplicity 2 [count/min]                                                                                             
i6     multiplicity 3 [count/min]                                                                                             
i6     multiplicity 4 [count/min]                                                                                             
i6     multiplicity 5 [count/min]                                                                                             
i5     multiplicity 6 [count/min]                                                                                             
i5     multiplicity 7 [count/min]                                                                                             
i5     multiplicity 8 [count/min]                                                                                             
------ --------------------------                                                                                             
f8.2   pressure [hPa]                                                                                                         
f7.2   temperature [deg C]                                                                                                    
------ pressure uncorrected -----                                                                                             
i8     total counts   [count/min]                                                                                             
i7     multiplicity 1 [count/min]                                                                                             
i7     multiplicity 2 [count/min]                                                                                             
i6     multiplicity 3 [count/min]                                                                                             
i6     multiplicity 4 [count/min]                                                                                             
i6     multiplicity 5 [count/min]                                                                                             
i5     multiplicity 6 [count/min]                                                                                             
i5     multiplicity 7 [count/min]                                                                                             
i5     multiplicity 8 [count/min]   


### pressure corrected hourly data                                                                                            
### /01h_corr/yyyymm/yyyymmdd.txt                                                                                             
format description                                                                                                            
------ ---------------------------                                                                                            
a19    start time of record                                                                                                   
------ pressure corrected --------                                                                                            
i10    total counts   [count/hour]                                                                                            
i9     multiplicity 1 [count/hour]                                                                                            
i9     multiplicity 2 [count/hour]                                                                                            
i8     multiplicity 3 [count/hour]                                                                                            
i8     multiplicity 4 [count/hour]                                                                                            
i8     multiplicity 5 [count/hour]                                                                                            
i7     multiplicity 6 [count/hour]                                                                                            
i7     multiplicity 7 [count/hour]                                                                                            
i7     multiplicity 8 [count/hour]                                                                                            
------ ---------------------------                                                                                            
f8.2   pressure [hPa]                                                                                                         
f7.2   temperature [deg C]                                                                                                    
------ pressure uncorrected ------                                                                                            
i10    total counts   [count/hour]                                                                                            
i9     multiplicity 1 [count/hour]                                                                                            
i9     multiplicity 2 [count/hour]                                                                                            
i8     multiplicity 3 [count/hour]                                                                                            
i8     multiplicity 4 [count/hour]                                                                                            
i8     multiplicity 5 [count/hour]                                                                                            
i7     multiplicity 6 [count/hour]                                                                                            
i7     multiplicity 7 [count/hour]                                                                                            
i7     multiplicity 8 [count/hour] 

    """

    data = []    
    if data_type in ["1m", "ori", "rev"]:
        data_type = "1m"
        data_path = '~/DATA/TIBET/Min_corr'
        interval = 60  # calculate actual interval?
    elif data_type in["1h"]:
        interval = 3600  # hour data
        data_path = '~/DATA/TIBET/Hourly_corr'
    elif data_type in["env", "meta"]:
        return data
        raise ValueError  # not yet defined for env and meta
    else:
        print "unknown data:", data_type
        raise ValueError

    # ~/DATA/TIBET/Min_corr/200601/20060101.txt
    data_path = os.path.expanduser(data_path)
    filename = "%s/%04i%02i/%04i%02i%02i.txt" % (data_path, year, month, year, month, day)

    if options.verbose:
        print "opening", filename
    try:
        myfile = open(filename, 'rb')
    except IOError:
        print "file not found", filename
        return data        

    line = True
    while line:
        line = myfile.readline()
        if len(line) < 60:
            break
        row = line.split()
        date = row[0]
        time = row[1]
        (year, month, day) = date.split("/")
        (hour, minute, second) = time.split(":")
        year = int(year)
        month = int(month)
        day = int(day)
        hour = int(hour)
        minute = int(minute)
        second = int(second)

        if second > 0:
            print "warning: invalid timestamp", date, time, "\t skipping data"
            #break  # invalid data
        timestamp = datetime(year, month, day, hour, minute, 0)  # seconds must be == 0
        
        corr = round(float(row[2])/interval, 3)
        press = round(float(row[11]), 2)
        uncorr = round(float(row[13])/interval, 3)
        uncorr = check_data(uncorr, cr_min=10., cr_max=10000.)
        press = check_pressure(press, p_min=500., p_max=700.)
        corr = check_data(corr, cr_min=10., cr_max=10000.)
        ceff = corr

        if options.verbose:
            print "DATA", timestamp, corr, press, uncorr
        # order as in NMDB
        data.append([timestamp, interval, uncorr, ceff, corr, press])

    return data

## --------------------------------------------------------------------------
def main():

    import optparse
    parser = optparse.OptionParser("usage: %prog [options]")
    parser.add_option("-v", dest="verbose", default=0, action="count",
            help="increment output verbosity; may be specified multiple times")
    (options, args) = parser.parse_args()

    print "This is the tibet module."

    station = "tibt"
    data_type="1h"
    data_type="1m"
    year = 2006
    month = 1
    day = 1

    #data = read_rev(options, station, data_type, year, month, day)
    data = read_rt(options, station, data_type, year, month, day)
    for line in data:
        print line
# --------------------------------------------------------------------------
if __name__ == '__main__':
    main()
